package com.xcgame.oh;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

/**
 * Created by Administrator on 2017/4/1.
 */

public class SyncOkHttp {

    private OkHttpClient client;

    public SyncOkHttp() {
        client = new OkHttpClient.Builder().
                connectTimeout(5, TimeUnit.SECONDS).
                readTimeout(15, TimeUnit.SECONDS).
                writeTimeout(15, TimeUnit.SECONDS)
                .build();
    }

    /**
     * @param connectTimeout 连接超时（秒钟）
     */
    public SyncOkHttp(int connectTimeout, int readTimeout, int writeTimeout) {
        client = new OkHttpClient.Builder().connectTimeout(connectTimeout, TimeUnit.SECONDS).readTimeout(readTimeout, TimeUnit.SECONDS).
                writeTimeout(writeTimeout, TimeUnit.SECONDS).build();
    }

    public String get(Context context, String url) {
        try {
            Request request = new Request.Builder().url(url).build();
            Call call = client.newCall(request);
            Response response = call.execute();
            if (response.isSuccessful()) {
                String result = response.body().string();
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String post(String url, Map<String, String> params) {
        try {
            FormBody.Builder builder = new FormBody.Builder();
            if (params != null) {
                for (Map.Entry entry : params.entrySet()) {
                    builder.add(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
                }
            }

            Request request = new Request.Builder().url(url).post(builder.build()).build();
            Call call = client.newCall(request);

            Response response = call.execute();
            if (response.isSuccessful()) {
                String result = response.body().string();
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String uploadFile(String url, Map<String, String> params, File file) {

        try {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.setType(MultipartBody.FORM);

            if (params != null) {
                for (Map.Entry entry : params.entrySet()) {
                    builder.addFormDataPart(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
                }
            }

            if (file != null && file.exists()) {
                RequestBody requestBody = RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), file);
                builder.addFormDataPart("file", file.getName(), requestBody);
            }

            Request request = new Request.Builder().url(url).post(builder.build()).build();
            Call call = client.newCall(request);

            Response response = call.execute();
            if (response.isSuccessful()) {
                String result = response.body().string();
                return result;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

}
